package day03;

import util.JdbcUtil;

import java.io.*;
import java.sql.*;

public class TestBlob {

    public static void main(String[] args) {
//        insert();

        select();

    }


    public static void insert(){
        Connection con=null;
        PreparedStatement ps=null;
        InputStream in=null;
        try{
            con= JdbcUtil.getConnection();
            con.setAutoCommit(false);

            String sql=new StringBuffer()
                    .append("insert into t_blob ")
                    .append("    (file_path,file,file_name) ")
                    .append("values ")
                    .append("    (?,?,?) ")
                    .toString();

            ps=con.prepareStatement(sql);


            ps.setString(1,"C:\\Users\\Administrator\\Desktop\\image\\a.jpg");


            in=new FileInputStream("C:\\Users\\Administrator\\Desktop\\image\\b.jpg");
            ps.setBinaryStream(2,in);

            ps.setString(3,"b.jpg");

            ps.executeUpdate();


            con.commit();
        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally{
            if(in!=null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            JdbcUtil.close(null,ps,con);
        }
    }


    public static void select(){
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try{
            con= JdbcUtil.getConnection();
            con.setAutoCommit(false);


            String sql=new StringBuffer()
                    .append("select * ")
                    .append("from t_blob ")
                    .append("where id=? ")
                    .toString();

            ps=con.prepareStatement(sql);

            ps.setInt(1,1);

            rs=ps.executeQuery();

            if(rs.next()){
                String filePath=rs.getString("file_path");
//                System.out.println("filePath = " + filePath);
//                File f=new File(filePath);
//                f.renameTo(new File("C:\\Users\\Administrator\\Desktop\\img\\a.jpg"));
                
                
                
                String fileName=rs.getString("file_name");
                System.out.println("fileName = " + fileName);
                
                Blob blob=rs.getBlob("file");

                try(InputStream in=blob.getBinaryStream();
                    FileOutputStream out=new FileOutputStream("C:\\Users\\Administrator\\Desktop\\img\\"+fileName)){

                    byte[] b=new byte[2048];

                    int n;

                    while((n=in.read(b))!=-1){
                        out.write(b,0,n);
                    }

                }catch (Exception e){
                    throw e;
                }


                
                
            }

            con.commit();
        }catch (Exception e){
            e.printStackTrace();
            try {
                con.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }finally{
            JdbcUtil.close(rs,ps,con);
        }
    }






















}
